#region 属性
const DebuggerMessage = preload("res://addons/State_Chart/util/editor_debugger/editor_debugger_message.gd")
var _chart:StateChart
var _ignore_transition:bool = true
var _ignore_events:bool = true
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
#endregion
#region 公共方法
#endregion
#region 私有方法
func _register_settings_updates():
	if not _chart.is_inside_tree():
		return
	EngineDebugger.register_message_capture(DebuggerMessage.SETTINGS_UPDATED_MESSAGE + str(_chart.get_path()), _on_settings_updated)
func _unregister_settings_updates():
	if not _chart.is_inside_tree():
		return
		EngineDebugger.unregister_message_capture(DebuggerMessage.SETTINGS_UPDATED_MESSAGE + str(_chart.get_path()))
func _on_settings_updated(key:String,data:Array) -> bool:
	_ignore_events = data[0]
	_ignore_transition = data[1]
	return true
func _prepare():
	_chart.event_received.connect(_on_event_received)
	for child in _chart.get_children():
		if child is State:
			_prepare_state(child)
func _prepare_state(state:State):
	state.state_entered.connect(_on_state_entered.bind(state))
	state.state_exited.connect(_on_state_exited.bind(state))
	state.transition_pending.connect(_on_transition_pending.bind(state))
	DebuggerMessage.state_updated(_chart,state)
	for child in state.get_children():
		if child is State:
			_prepare_state(child)
		if child is Transition:
			child.taken.connect(_on_transition_taken.bind(state,child))
func _on_transition_taken(source:State,transition:Transition):
	if _ignore_transition:
		return
	DebuggerMessage.transition_taken(_chart,source,transition)
func _on_event_received(event:StringName):
	if _ignore_events:
		return
	DebuggerMessage.event_received(_chart, event)
func _on_state_entered(state:State):
	DebuggerMessage.state_entered(_chart, state)
func _on_state_exited(state:State):
	DebuggerMessage.state_exited(_chart, state)
func _on_transition_pending(num1, remaining, state:State):
	DebuggerMessage.transition_pending(_chart, state, state.pending_transition, remaining)
#endregion
#region 生命周期函数
func _init(chart:StateChart) -> void:
	_chart = chart
	if not is_instance_valid(_chart):
		push_error("StateChart无效")
	_register_settings_updates()
	DebuggerMessage.state_chart_added(_chart)
	_prepare()
func _notification(what: int) -> void:
	match (what):
		Node.NOTIFICATION_ENTER_TREE:
			DebuggerMessage.state_chart_added(_chart)
			_register_settings_updates()
		Node.NOTIFICATION_UNPARENTED:
			DebuggerMessage.state_chart_removed(_chart)
			_unregister_settings_updates()
#endregion
